package cn.onein.edu.web.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.Item;
import cn.onein.edu.biz.bean.Order;
import cn.onein.edu.biz.bean.Product;
import cn.onein.edu.biz.bean.User;
import cn.onein.edu.common.OrderStatusType;
import cn.onein.edu.common.PaymentType;
import cn.onein.edu.common.ProductPeriodType;
import cn.onein.edu.util.DateUtil;
import cn.onein.edu.web.dao.PaymentDao;
import cn.onein.edu.web.dao.UserCenterDao;
import cn.onein.edu.web.dao.UserDao;
import cn.onein.edu.web.service.PaymentService;

/**
 * 支付service
 * @author Upton
 *
 */
@Service
public class PaymentServiceImpl implements PaymentService {

	@Autowired
	private PaymentDao paymentDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserCenterDao userCenterDao;
	
	private final static int LAST_DAY_KEY = -1;
	
	private final static int NEXT_WEEK_KEY = 6;

	@Override
	public Product getProductById(Long id) {
		return paymentDao.getProductById(id);
	}

	@Override
	public Order insertOrderAndItem(Order order,Item item) {
		paymentDao.insertOrder(order);
		Order orderBean = paymentDao.getOrderByOrderNo(order.getOrderNo());
		item.setOrderId(orderBean.getId());
		paymentDao.insertItem(item);
		
		return paymentDao.getOrderByOrderNo(order.getOrderNo());
	}

	@Override
	public List<Order> getOrderByUserId(Long userId) {
		return paymentDao.getOrderByUserId(userId);
	}

	@Override
	public List<Item> getItemByOrderId(Long orderId) {
		return paymentDao.getItemByOrderId(orderId);
	}

	@Override
	public List<Product> getProductByType(Integer type) {
		return paymentDao.getProductByType(type);
	}

	@Override
	public Order getOrderByOrderNo(String orderNo) {
		return paymentDao.getOrderByOrderNo(orderNo);
	}

	@Override
	public void updateOrderStatusByOrderNo(String orderNo,OrderStatusType type) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("orderNo", orderNo);
		param.put("orderStatus", type.ordinal());
		
		paymentDao.updateOrderStatusByOrderNo(param);
	}

	@Override
	public Order getLastOrderByUserId(Long userId) {
		return paymentDao.getLastOrderByUserId(userId);
	}

	@Override
	public void orderSuccessByOrderNo(String orderNo, String payNo,PaymentType paymentType) {
        Map<String,Object> orderParam = new HashMap<String,Object>();
        Map<String,Object> userParam = new HashMap<String,Object>();
        Map<String,Object> userActionParam = new HashMap<String,Object>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date levelExpiresDate = null;
        ProductPeriodType period = null;
        
        orderParam.put("orderStatus", OrderStatusType.PaySuccess.ordinal());
        orderParam.put("paymentType", paymentType.ordinal());
        orderParam.put("paymentNo", payNo);
        orderParam.put("updateDate", new Date());
        orderParam.put("orderNo", orderNo);
        
        Order order = paymentDao.getOrderByOrderNo(orderNo);
        if(order == null){
        	return;
        }
        List<Item> items = paymentDao.getItemByOrderId(order.getId());
        for(Item item : items){
        	Product po = paymentDao.getProductById(item.getProductId());
        	if(po == null){
        		continue;
        	}
        	period = po.getProductPeriodType();
        }
        
        User user = userDao.getUserById(order.getUserId());
        if(user == null || period == null){
        	return;
        }
        if(user.getLevelExpires() == null || user.getLevelExpires().before(new Date())){
        	levelExpiresDate = DateUtil.getDateByPeriod(new Date(), period);
        }else {
            levelExpiresDate = DateUtil.getDateByPeriod(user.getLevelExpires(), period);
        }
        
        userParam.put("levelExpiresDate", levelExpiresDate);
        userParam.put("id", order.getUserId());
        
        userActionParam.put("userId", order.getUserId());
        userActionParam.put("beginDate", DateUtil.getDateBydays(new Date(), LAST_DAY_KEY));
        userActionParam.put("endDate", DateUtil.getDateBydays(new Date(), NEXT_WEEK_KEY));
        
        paymentDao.orderSuccessByOrderNo(orderParam);

        userDao.updateUserLevelExpries(userParam);
        
        userCenterDao.updateUserActionActive(userActionParam);
	}

	@Override
	public void updateOrderPaymentTypeByOrderNo(String orderNo,
			PaymentType aymentType) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("paymentType", aymentType.ordinal());
		param.put("orderNo", orderNo);
		
		paymentDao.updateOrderPaymentTypeByOrderNo(param);
	}
	
}
